const app = getApp();
const ajax = app.myRequest()
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
    data: {
        code: false
    },

    getmembershipCard() {
        var _this = this
        ajax.getRequest('/User/card').then((res) => {
            if (res.data.data.data.length == 0) {
                Dialog.alert({ message: '您暂时没有会员卡' })
            }
            _this.setData({
                card: res.data.data.data,
                current_page: res.data.data.current_page
            })
        })
    },

    Write(e) {
        var index = e.currentTarget.dataset.index
        var card = this.data.card[index]
        if (card.status == 3) {
            Dialog.alert({ message: '您的会员卡已到期' })
        } else {
            this.setData({ index, code: true })
            this.QRCode()
        }
    },

    QRCode() {
        var index = this.data.index
        var cardId = this.data.card[index].id
        var page = 'pages/personal/systemSettings/index'
        var type = 1
        var gymnasium_id = this.data.card[index].gymnasium_id
        var scene = cardId + '&' + type + '&' + gymnasium_id
        var width = 300
        var _this = this
        ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
            var QRcode = res.data.data
            _this.setData({ QRcode })
        })


    },
    closeCode() {
        this.setData({ code: false })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getmembershipCard()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    // 加载
    onReachBottom: function () {
        this.branch()
    },
    branch() {
        var _this = this
        var current_page = this.data.current_page
        wx.showLoading()
        setTimeout(function () { wx.hideLoading() }, 3000)

        ajax.getRequest('/User/card', { page: current_page + 1 }).then((res) => {
            var card = _this.data.card.concat(res.data.data.data)
            _this.setData({
                card: card,
                current_page: res.data.data.current_page,
                total: res.data.data.total
            })
        })
    },
})